package org.exam.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.exam.blog.domain.Log;
import org.exam.blog.mapper.LogMapper;
import org.exam.blog.service.ILogService;
import org.exam.system.query.LogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class LogServiceImpl implements ILogService {
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    @Transactional
    public void add(Log log) {
        logMapper.add(log);
        //添加
        mongoTemplate.save(log);

    }

    @Override
    @Transactional
    public void delete(Long id) {
        logMapper.delete(id);
        //条件删除
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, Log.class);
    }

    @Override
    public Log get(Long id) {
        return logMapper.get(id);
    }

    @Override
    public List<Log> getAll() {
        return logMapper.getAll();
    }



    @Override
    @Transactional
    public void update(Log log) {
        logMapper.update(log);
        //MongoDB修改
    }

    @Override
    public void patchDelete(Long[] ids) {
        logMapper.patchDelete(ids);
    }

    @Override
    public PageInfo<Log> queryPage(LogQuery logQuery) {
        PageHelper.startPage(logQuery.getCurrentPage(), logQuery.getPageSize());
        List<Log> data = logMapper.queryPage(logQuery);
        return new PageInfo<>(data);
    }



}
